package com.china08.yxyapi.api.manage.tec.teaching.scoreport.compareexam;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.mg.scoreport.Exam;
import com.china08.yxyapi.entity.mg.scoreport.ExamStudent;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.scoreport.ExamRepository;
import com.china08.yxyapi.repository.mg.scoreport.ExamStudentRepository;
import com.china08.yxyapi.util.DateUtils;

/**
 * 获取同年级同时间段考试信息
 * 
 */
@CrossOrigin
@RestController
public class CompareExamClassLstController {

    @Autowired
    private ExamRepository examRepository;

    @Autowired
    private ExamStudentRepository examStudentRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/scoreport/compareexam/classlst", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel model) throws ParseException {

        Exam exam = examRepository.findOne(model.id);
        Assert.notNull(exam, "考试信息不存在");

        List<Exam> examLst = examRepository.findByClassIdInAndExamDateAndSubjectAndTotalScore(model.classIdLst,
                exam.getExamDate(), exam.getSubject(), exam.getTotalScore());

        examLst.remove(exam);

        List<RespModel> modelsLst = new ArrayList<RespModel>();
        for (Exam e : examLst) {
            List<ExamStudent> examStudents = examStudentRepository.findByExam(e);
            if (examStudents.size() == 0) {
                continue;
            }
            RespModel respModel = new RespModel();
            respModel.id = e.getId();
            respModel.name = e.getName();
            respModel.className = e.getClassName();
            respModel.subjectName = e.getSubject();
            respModel.examDate = DateUtils.formatDate2(DateUtils.formatStrDate4(e.getExamDate()));
            respModel.teacherNick = e.getTeacherNick();
            modelsLst.add(respModel);
        }
        return modelsLst;

    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiReqModel {
        // 进行比较的考试id
        public String id;
        // 同年级下的班级id集合
        public List<String> classIdLst;
    }

    /**
     * 响应参数
     * 
     */
    public static class RespModel extends YxyApiRespModel {
        // 考试id
        public String id;
        // 考试名称
        public String name;
        // 班级名称
        public String className;
        // 学科名称
        public String subjectName;
        // 考试时间
        public String examDate;
        // 发布用户名称
        public String teacherNick;

    }

}
